package com.djmw.mouse.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.djmw.mouse.service.constants.Constants;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Entity
@Table(name = "t_cv_education")
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
@JsonIgnoreProperties(ignoreUnknown = true)
public class CvEducation extends IdEntity {

    private CvLang cvLang;
    private Integer order;
    private Integer startDate; // YYYYMM
    private Integer endDate = 0; // YYYYMM
    private String school;
    private String major;
    private Integer degree;
    private String majorDescription;
    private Character isOversea = 'n';

    public CvEducation() {
    }

    @ManyToOne
    @JoinColumn(name = "c_cv_lang_id")
    public CvLang getCvLang() {
        return cvLang;
    }
    public void setCvLang(CvLang cvLang) {
        this.cvLang = cvLang;
    }

    @Column(name = "c_order")
    @JsonProperty("order")
    public Integer getOrder() {
        return order;
    }
    public void setOrder(Integer order) {
        this.order = order;
    }

    @Column(name = "c_start_date")
    @JsonProperty("startDate")
    public Integer getStartDate() {
        return startDate;
    }
    public void setStartDate(Integer startDate) {
        this.startDate = startDate == null ? 0 : startDate.intValue();
    }

    @Column(name = "c_end_date")
    @JsonProperty("endDate")
    public Integer getEndDate() {
        return endDate;
    }
    public void setEndDate(Integer endDate) {
        this.endDate = endDate == null ? 0 : endDate.intValue();
    }

    @Column(name = "c_school")
    @JsonProperty("school")
    public String getSchool() {
        return school;
    }
    public void setSchool(String school) {
        this.school = school;
    }

    @Column(name = "c_major")
    @JsonProperty("major")
    public String getMajor() {
        return major;
    }
    public void setMajor(String major) {
        this.major = major;
    }

    @Column(name = "c_degree")
    @JsonProperty("degree")
    public Integer getDegree() {
        return degree;
    }
    public void setDegree(Integer degree) {
        this.degree = degree == null ? 0 : degree.intValue();
    }
    @Transient
    @JsonProperty("degreeName")
    public String getDegreeName() {
        return degree == null || degree == 0 || degree >= Constants.EDUCATIONS.size() ? "" : Constants.EDUCATIONS.get(
                degree).getName();
    }

    @Column(name = "c_major_description")
    @JsonProperty("majorDescription")
    public String getMajorDescription() {
        return majorDescription;
    }
    public void setMajorDescription(String majorDescription) {
        this.majorDescription = majorDescription;
    }

    @Column(name = "c_is_oversea")
    @JsonProperty("oversea")
    public Character getIsOversea() {
        return isOversea;
    }
    public void setIsOversea(Character isOversea) {
        this.isOversea = isOversea == null ? 'n' : isOversea.charValue();
    }

    @Transient
    public String getJson() {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return "{}";
        }
    }

}
